<template>
  <div>
    <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
        <!-- 搜索 -->
        <el-input placeholder="请输入内容" class="input-with-select" v-model="queryinfo.query" style="width:400px;" @input="getgoodslist()">
            <el-button slot="append" icon="el-icon-search" @click="getgoodslist()"></el-button>
        </el-input>
        <!-- 添加商品 -->
        <el-button type="primary" style="margin-left:20px;" @click="goodsadd">添加商品</el-button>

          <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
              type="index"
              label="#"
              width="40px">
            </el-table-column>
            <el-table-column
              prop="goods_name"
              label="商品名称"
              width="500">
            </el-table-column>
            <el-table-column
              prop="goods_price"
              label="商品价格（元）">
            </el-table-column>
            <el-table-column
              prop="goods_weight"
              label="商品重量">
            </el-table-column>
            <el-table-column
              prop="goods_number"
              label="商品数量">
            </el-table-column>
            <el-table-column
              width="150"
              label="创建时间">
              <template slot-scope="scope">
                <div>
                  {{scope.row.add_time | DateTime}}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200px">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="Edit(scope.row.goods_id)">编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="Delete(scope.row.goods_id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 编辑 -->
          <el-dialog title="编辑商品" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm1" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="商品名称" prop="goods_name">
                <el-input v-model="ruleForm1.goods_name"></el-input>
              </el-form-item>
              <el-form-item label="商品价格" prop="goods_price">
                <el-input v-model="ruleForm1.goods_price"></el-input>
              </el-form-item>
              <el-form-item label="商品重量" prop="goods_weight">
                <el-input v-model="ruleForm1.goods_weight"></el-input>
              </el-form-item>
              <el-form-item label="商品数量" prop="goods_number">
                <el-input v-model="ruleForm1.goods_number"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updata('ruleForm')">确定</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </el-dialog>

          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryinfo.pagenum"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="queryinfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { goodsApi,delGoodsApi,editGoodsApi,updateGoodsApi } from "../../http/api";
export default {
  data() {
    return {
      tableData:[],
      dialogFormVisible:false,
      queryinfo:{
        query:'',
        pagenum:1,
        pagesize:10,
      },
      total:0,
      ruleForm1: {},
      rules: {
        goods_name: [
          { required: true, message: '请修改商品名称', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请修改商品价格', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请修改商品重量', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '请修改商品数量', trigger: 'blur' },
        ],
      }
    }
  },
  created(){
    this.getgoodslist()
  },
  methods: {
    // 渲染
    async getgoodslist(){
      const res = await goodsApi(this.queryinfo)
      console.log(res);
      this.tableData = res.goods
      this.total = res.total
    },
    // 添加
    goodsadd(){
      this.$router.push('/goodsadd')
    },
    // 删除
    Delete(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delGoodsApi(id).then(res=>{
          console.log(res);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getgoodslist()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    // 编辑
    async Edit(id){
      // console.log(id);
      this.dialogFormVisible = true
      const res = await editGoodsApi(id)
      console.log(res);
      this.ruleForm1 = res
    },
    // 编辑提交
    updata() {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) {
          return false
        }
        const {goods_id,goods_name,goods_price,goods_number,goods_weight} = this.ruleForm1
        // console.log(goods_id);
        const res = await updateGoodsApi(goods_id,{goods_name:goods_name,goods_price:goods_price,goods_number:goods_number,goods_weight:goods_weight})
        console.log(res);
        this.getgoodslist()
        this.dialogFormVisible = false
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryinfo.pagesize = val
      this.getgoodslist()
    },
    handleCurrentChange(val) {
       console.log(`当前页: ${val}`);
       this.queryinfo.pagenum = val
      this.getgoodslist()
    },
  }
}
</script>

<style>
.box-card{
  margin: 10px 0;
}
.el-table{
  margin: 10px;
}
</style>